#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include "SharedMemory.h"

/*  Process 3 tries to lock attach to same mem as process1,2 , then lockTimeOut every period until success   *
 *  and prints the content of the shared memory, unlock and deattach                                        */
int main()
{
    SM*             sm              ;
    void*           memory          ;
    unsigned long   timeout = 1000  ;

    /*printf("Unlock operation: %d\n",SMUnlock(sm));*/
    sleep(3);
    sm      = SMAttach("smtest4")   ;
    memory  = SMGet(sm)             ;

    printf("Process3: LockTimeOut operation: every %ld msec \n", timeout);

    while(SMLockTimeOut(sm, timeout))
    {
        printf("Process3: Fail to lock\n");
    }
    printf("Process3: Now is locked\n");

    sleep(2);
    printf("Process3: Printing from the shared memory\n");
    printf("%s\n", (char*)memory);

    printf("Process3: Unlock operation: %d\n",SMUnlock(sm));
    SMDetach(sm);

    return 0;
}
